﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Register.Model;
using Register.BLL;
using System.Web.Script.Serialization;
using Register.Command;
using System.Text;

namespace RegisterWeb.Manager.PatientManager.ashx
{
    /// <summary>
    /// PatientManagerServer 的摘要说明
    /// </summary>
    public class PatientManagerServer : IHttpHandler
    {
        public String IntimateString(String intimate)
        {
            int lenth = intimate.Length;
            int count=(int)lenth/11*3;
            String new_first = intimate.Substring(0, count);
            String new_last = intimate.Substring(lenth - count - 1, count);
            String new_intimate="";
            new_intimate += new_first;
            for (int i = 0; i < lenth - count * 2 - 1; i++)
            {
                new_intimate += "*";
            }
            new_intimate += new_last;
            return new_intimate;
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            String action = context.Request["action"].ToString();
            if (action.Equals("list"))
            {
                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());
                List<Patient> list = new List<Patient>();
                list = PatientUserInfoBLL.GetPagedPatientUserInfo(row, page, " PatientUserInfo_State=1 ");

                List<PatientJSON> json = new List<PatientJSON>();
                foreach (Patient patient in list)
                {
                    if (patient.PatientUserInfo_MedicareCard == "1")
                        patient.PatientUserInfo_MedicareCard = "有";
                    if (patient.PatientUserInfo_MedicareCard == "0")
                        patient.PatientUserInfo_MedicareCard = "无";
                    json.Add(new PatientJSON(patient.PatientUserInfo_ID, patient.PatientUserInfo_Name, patient.PatientUserInfo_Password, IntimateString(patient.PatientUserInfo_Phone), patient.PatientUserInfo_BirthDay.ToString(), patient.PatientUserInfo_Sex, patient.PatientUserInfo_Mail, IntimateString(patient.PatientUserInfo_IDCrad), patient.PatientUserInfo_UserType, patient.PatientUserInfo_MedicareCard, patient.PatientUserInfo_AddTime.ToString(), patient.PatientUserInfo_NotComeTime.ToString(), patient.PatientUserInfo_RrgIP, patient.PatientUserInfo_State, getUserType(patient.PatientUserInfo_UserType)));
                }

                PatientListJSON patientListJSON = new PatientListJSON();
                patientListJSON.total = PatientUserInfoBLL.GetPatientUserInfoCount(null);
                patientListJSON.rows = json;
                

                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(patientListJSON));
            }

            else if (action.Equals("edit"))
            {
                Patient patient = new Patient();
                patient.PatientUserInfo_ID = context.Request["ID"];
                patient.PatientUserInfo_UserType = context.Request["MyUserType"];
                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(new message(PatientUserInfoBLL.UpdatePatientUserInfo(patient), "用户等级修改成功！")));

            }
            else if (action.Equals("search"))
            {
                string UserType = context.Request["UserType"];
                string NotComeTime = context.Request["NotComeTime"];
                string Begin = context.Request["Begin"];
                string End=context.Request["End"];
                PatientListJSON patientListJSON = new PatientListJSON();
                patientListJSON.rows = new List<PatientJSON>();

                int row = int.Parse(context.Request["rows"].ToString());
                int page = int.Parse(context.Request["page"].ToString());

                StringBuilder strBuilder = new StringBuilder();
                strBuilder.Append(" PatientUserInfo_State='1' ");
                if (!String.IsNullOrEmpty(NotComeTime))
                {
                    strBuilder.Append(" and  PatientUserInfo_NotComeTime='").Append(NotComeTime).Append("' ");
                }
                if (!String.IsNullOrEmpty(UserType))
                {
                    strBuilder.Append(" and PatientUserInfo_UserType='").Append(UserType).Append("' ");
                }
                if (!(String.IsNullOrEmpty(Begin) || String.IsNullOrEmpty(End)))
                {
                    strBuilder.Append(" and PatientUserInfo_AddTime  Between '").Append(Begin).Append("' and '").Append(End).Append("'");
                }
                List<Patient> list = PatientUserInfoBLL.GetPagedPatientUserInfo(row, page, strBuilder.ToString());
                patientListJSON.total = PatientUserInfoBLL.GetPatientUserInfoCount(strBuilder.ToString());


                foreach (Patient patient in list)
                {
                    patientListJSON.rows.Add(new PatientJSON(patient.PatientUserInfo_ID, patient.PatientUserInfo_Name, patient.PatientUserInfo_Password, IntimateString(patient.PatientUserInfo_Phone), patient.PatientUserInfo_BirthDay.ToString(), patient.PatientUserInfo_Sex, patient.PatientUserInfo_Mail, IntimateString(patient.PatientUserInfo_IDCrad), patient.PatientUserInfo_UserType, patient.PatientUserInfo_MedicareCard, patient.PatientUserInfo_AddTime.ToString(), patient.PatientUserInfo_NotComeTime.ToString(), patient.PatientUserInfo_RrgIP, patient.PatientUserInfo_State, getUserType(patient.PatientUserInfo_UserType)));
                }


                JavaScriptSerializer jss = new JavaScriptSerializer();
                context.Response.Write(jss.Serialize(patientListJSON));
            }
            else if (action.Equals("UserType"))
            {
                List<Parameter> list;
                JavaScriptSerializer jss = new JavaScriptSerializer();

                list = ParamterHelper.GetParametersByType("Patient_UserLevel");
                context.Response.Write(jss.Serialize(list));
            }

        }
        public String getUserType(String UserType)
        {
            String TextUserType = "";
            List<Parameter> list;
            JavaScriptSerializer jss = new JavaScriptSerializer();

            list = ParamterHelper.GetParametersByType("Patient_UserLevel");
            foreach (Parameter parameter in list)
            {
                if (parameter.SystemParameter_ID == UserType)
                    TextUserType = parameter.SystemParameter_Name;
            }
            return TextUserType;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        class message
        {
            public message(bool success, string msg)
            {
                this.msg = msg;
                this.success = success;
            }
            public bool success { get; set; }
            public string msg { get; set; }
        }

        class PatientListJSON
        {

            public int total { get; set; }
            public List<PatientJSON> rows { get; set; }

        }

        class PatientJSON
        {
            public String Name { get; set; }
            public String ID { get; set; }
            public String Password { get; set; }
            public String Phone { get; set; }
            public String BirthDay { get; set; }
            public String Sex { get; set; }
            public String Mail { get; set; }
            public String IDCrad { get; set; }
            public String UserType { get; set; }
            public String MedicareCard { get; set; }
            public String AddTime { get; set; }
            public String NotComeTime { get; set; }
            public String RrgIP { get; set; }
            public String State { get; set; }
            public String TextUserType { get; set; }

            public PatientJSON(String ID, String Name, String Password, String Phone, String BirthDay, String Sex, String Mail, String IDCrad, String UserType, String MedicareCard, String AddTime, String NotComeTime, String RrgIP, String State, String TextUserType)
            {
                this.Name = Name;
                this.ID = ID;
                this.Password = Password;
                this.Phone = Phone;
                this.BirthDay = AddTime;
                this.Sex = Sex;
                this.Mail = Mail;
                this.IDCrad = IDCrad;
                this.UserType = UserType;
                this.MedicareCard = MedicareCard;
                this.AddTime = AddTime;
                this.NotComeTime = NotComeTime;
                this.RrgIP = RrgIP;
                this.State = State;
                this.TextUserType = TextUserType;
            }
        }

    }
}